package com.lpc.android.abc.base.base;

import android.app.Application;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.BallPulseView;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;
import com.lpc.android.abc.base.di.component.AppComponent;
import com.lpc.android.abc.base.di.component.DaggerAppComponent;
import com.lpc.android.abc.base.di.module.AppModule;
import com.lpc.android.abc.base.exception.CrashHandler;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by lpc on 2016/11/16.
 */

public class BaseApplication {

    private static Application mApplication;

    private static RefWatcher refWatcher;
    private static AppComponent appComponent;

    public static AppComponent getAppComponent() {
        return appComponent;
    }

    public static Application getApplication() {
        return mApplication;
    }

    public static RefWatcher getRefWatcher() {
        return refWatcher;
    }

    public static void onCreate(Application application) {

        mApplication = application;

        initLeakCanary();

        appComponent = DaggerAppComponent.builder().appModule(new AppModule(mApplication)).build();

        TwinklingRefreshLayout.setDefaultHeader(SinaRefreshView.class.getName());
        TwinklingRefreshLayout.setDefaultFooter(BallPulseView.class.getName());

        //先初始化自身的异常处理逻辑，否则与友盟异常机制冲突
        CrashHandler crashHandler = new CrashHandler();
        crashHandler.init();

        MobclickAgent.setScenarioType(mApplication, MobclickAgent.EScenarioType.E_UM_NORMAL);
    }

    public static void onTerminate(){

        mApplication = null;

        refWatcher = null;
    }

    private static void initLeakCanary() {

        if (LeakCanary.isInAnalyzerProcess(mApplication)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        refWatcher = LeakCanary.install(mApplication);
    }


}
